package com.jumper.terry.galleydemo;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


    ViewPager mViewpager;


    LinearLayout llContainer;

    ViewPager.OnPageChangeListener mOnPageChangeListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        llContainer = (LinearLayout)findViewById(R.id.llContainer);
        mViewpager = (ViewPager)findViewById(R.id.mViewpager);

        mViewpager.setOffscreenPageLimit(5);

        mViewpager.setAdapter(new ViewPagerAdapter());
        mViewpager.setPageMargin(6);

        mViewpager.setPageTransformer(false,new DepthPageTransformer());

        llContainer.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // dispatch the events to the ViewPager, to solve the problem that we can swipe only the middle view.
                return mViewpager.dispatchTouchEvent(event);
            }
        });


        mOnPageChangeListener = new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.d("Terry","onPageScrolled positionOffset---"+positionOffset   );
                Log.d("Terry","onPageScrolled positionOffsetPixels---"+positionOffsetPixels   );
            }

            @Override
            public void onPageSelected(int position) {
                Log.d("Terry","this is postion---"+position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        };

        mViewpager.addOnPageChangeListener(mOnPageChangeListener);

    }






    class ViewPagerAdapter extends PagerAdapter{

        private List<String> list;

        int dp100 = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
              100,MainActivity.this.getResources().getDisplayMetrics()
        );



        public ViewPagerAdapter(){
            list = new ArrayList<>();

            for (int i = 0 ;i < 100 ;i ++){
                list.add("X年"+i+"月");
            }

        }


        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }


        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View)object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            TextView tv = new TextView(MainActivity.this);
            tv.setTextColor(Color.BLACK);
            tv.setText(list.get(position));
            tv.setGravity(Gravity.CENTER);

            tv.setLayoutParams(new ViewGroup.LayoutParams(dp100,
                    ViewGroup.LayoutParams.MATCH_PARENT));

            container.addView(tv);
            return tv;
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mOnPageChangeListener != null){
            mViewpager.removeOnPageChangeListener(mOnPageChangeListener);
        }
    }
}
